from mcp.server.fastmcp import FastMCP

# 创建MCP服务实例
mcp = FastMCP("Calculator Service")

@mcp.tool()
def add(a: float, b: float) -> str:
    """计算两数之和"""
    return str(a + b)

@mcp.tool()
def subtract(a: float, b: float) -> str:
    """计算两数之差"""
    return str(a - b)

@mcp.tool()
def multiply(a: float, b: float) -> str:
    """计算两数之积"""
    from decimal import Decimal, getcontext
    getcontext().prec = 50  # 设置足够高的精度
    return str(Decimal(str(a)) * Decimal(str(b)))

@mcp.tool()
def divide(a: float, b: float) -> str:
    """计算两数之商"""
    try:
        if b == 0:
            return "错误: 除数不能为零"
        return str(a / b)
    except Exception as e:
        return f"计算错误: {str(e)}"

if __name__ == "__main__":
    mcp.run(transport="stdio")
